﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Photo;

namespace Photo.Module
{
    public partial class ucLogin : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Request.Cookies["remember"] != null)
                {
                    HttpCookie cookie = Request.Cookies.Get("UserNameAndPassword");
                    txtUsername.Text = cookie.Values["Username"].ToString();
                }
            }
            if (Session["IsLogin"] == null)
            {
                login_form.Visible = true;
                logged_form.Visible = false;
                lblLogOut.Visible = false;
            }
            if (Session["IsLogin"] != null)
            {
                LoginDO obj = new LoginBL().Select(new LoginDO { UserID = int.Parse(Session["IsLogin"].ToString()) });
                lblUsername.Text = obj.Password.ToString();
                txtPassword.Text = obj.Password.ToString();
                btnSignIn_OnClick(sender, e);
                //logged_form.Visible = true;
                //login_form.Visible = false;
                //lblLogOut.Visible = true;
               
            }
        }


        protected void btnSignIn_OnClick(object sender, EventArgs e)
        {
            string txtPass = txtPassword.Text;
            LoginDO obj = new LoginBL().SelectLogin(txtUsername.Text, txtPass);
            if (obj.UserID > 0)
            {
                Session["Username"] = obj.Username;
                Session["UserID"] = obj.UserID;
               
                lblUsername.Text = Session["Username"].ToString();

                logged_form.Visible = true;
                login_form.Visible = false;
                lblLogOut.Visible = true;
                Session["IsLogin"] = obj.UserID.ToString();
             
            }

            else
            {
                Functions.Alert("Sai tên đăng nhập hoặc mật khẩu!", Request.Url.AbsolutePath);
            }

            WriteCookie();
        }



        protected void WriteCookie()
        {
           
                HttpCookie cookie = new HttpCookie("remember", "yes");
                Response.Cookies.Add(cookie);
                cookie = new HttpCookie("UserNameAndPassword");
                cookie.Values.Add("Username", txtUsername.Text.Trim());
                Response.Cookies.Add(cookie);
           
        }

        protected void LogOut_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("/Default.aspx");
        }

        protected void xoaanh(object sender, EventArgs e)
        {

        }

    }
}